package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.result.ResultUtil;
import com.ctcemti.mdm.common.util.ExcelParseUtils;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.common.util.StringUtils;
import com.ctcemti.mdm.project.sys.entity.ResPerson;
import com.ctcemti.mdm.project.sys.service.IResPersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Api(value = "ResPersonController", description = "人员管理控制器类")
@RequestMapping(value="resPerson")
public class ResPersonController {

    @Autowired
    IResPersonService resPersonService;

    @PostMapping("insertOrUpdate")
    public Result insertOrUpdate(HttpServletRequest request, ResPerson resPerson){
        if(resPerson == null){
            return ResultUtil.error(ResultEnum.ZTSJ_1001);
        }
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        if(resPerson.getId() == null || "".equals(resPerson.getId())){
            resPerson.setCreateDate(new Date());
            resPerson.setCreator(loginId);
        }
        resPerson.setModifiedBy(loginId);
        resPerson.setModifyDate(new Date());
        return resPersonService.insertOrUpdate(resPerson);
    }

    @PostMapping("importLabours")
    public Result importLabours(@RequestParam("file") MultipartFile file){
        try{
            List<ResPerson> list = ExcelParseUtils.importExcel(file,0,1,ResPerson.class);
            for(ResPerson o:list){
                //查询job的值
                resPersonService.insertOrUpdate(o);
            }
        }catch(Exception e){
            return ResultUtil.error(ResultEnum.ZTSJ_1021);
        }
        return ResultUtil.success("导入成功");
    }

    @PostMapping(value="delete")
    public Result delete(String id){
        if(StringUtils.isNullOrEmpty(id)){
            return ResultUtil.error(ResultEnum.ZTSJ_1001);
        }
        //循环删除
        String[] ids = id.split(",");
        for(String temp:ids){
            if(!StringUtils.isNullOrEmpty(temp)){
                ResPerson resLabourCompany = new ResPerson();
                resLabourCompany.setId(temp);
                resPersonService.delete(resLabourCompany);
            }
        }
        return ResultUtil.success("删除成功");
    }

    @PostMapping(value="getResPerson")
    public Result getResPerson(String companyId,String page,String limit){
        Integer pageInt = page!=null&&!page.equals("")?Integer.parseInt(page):1;
        Integer limitInt = limit!=null&&!limit.equals("")?Integer.parseInt(limit):10;
        ResPerson resPerson= new ResPerson();
        resPerson.setCompanyId(companyId);
        return ResultUtil.success(resPersonService.getResPerson(resPerson,pageInt,limitInt));
    }

    /* *
     * @Author bxf
     * @Description
     * @Date 14:40 2019/10/23
     * @param request
     * @param response
     * @param entity
     * @return com.ctcemti.mdm.common.result.Result<com.ctcemti.mdm.project.sys.entity.ResPerson>
     */
    @ApiOperation(value = "根据劳务人员实体类获取list")
    @PostMapping("selectResPersonByEntity")
    @ApiImplicitParam(name="entity",value = "劳务人员实体类")
    public Result selectResPersonByEntity(HttpServletRequest request, HttpServletResponse response, ResPerson entity){
        List<ResPerson> list = new ArrayList<ResPerson>();
        list = resPersonService.selectResPersonCompany(entity);
        return new Result<>(ResultEnum.SUCCESS,list);
    }

}
